package com.bysj.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class ImageUploadController {
    @CrossOrigin
    @ResponseBody
    @PostMapping("/imageUpload")
    public String imageUpload(@RequestParam("file") MultipartFile file) throws IOException {
        String filePath = "D:\\img";
        //获取原始图片的拓展名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //新的文件名字
        String newFileName = UUID.randomUUID() +suffix;
        //封装上传文件位置的全路径
        File targetFile = new File(filePath,newFileName);
        //把本地文件上传到封装上传文件位置的全路径
        file.transferTo(targetFile);
        String url ="http://192.168.174.1:8080/image/" + newFileName;
        return url;
    }
}
